package com.shujia.day17;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.*;

/*
    登录注册案例
 */
public class IODemo1 {
    public static void init(){
        System.out.println("=============== 欢迎来到李刚博彩城！！ ================");
        System.out.println("-------------------");
        System.out.println("菜单：");
        System.out.println("1. 登录");
        System.out.println("2. 注册");
        System.out.println("3. 忘记密码");
        System.out.println("-------------------");
        Scanner sc = new Scanner(System.in);
        System.out.print("您要做的操作选项：");
        int choice = sc.nextInt();
        switch (choice){
            case 1:
                login();
                break;
            case 2:
                register();
                break;
            case 3:
                break;
            default:
                System.out.println("没有该选项...欢迎下次使用！");
        }
    }

    public static void register(){
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>注册操作进行中");
        Scanner sc = new Scanner(System.in);
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("######## 欢迎注册李刚博彩城 ^_< ########");
        System.out.print("请设置新用户名: ");
        String name = sc.nextLine();
        //TODO: 这里需要读取配置文件获取用户和密码
        Map<String, String> userMaps = getUserMaps();
        if(userMaps.containsKey(name)){
            System.out.println("注册失败！该用户名已经被使用.....");
        }else {
            while (true){
                System.out.print("请设置新用户的密码: ");
                String password = sc.nextLine();
                if("".equals(password)){
                    System.out.println("密码不能为空, 请重新输入！");
                }else {
                    saveUser(userMaps,name,password);
                    System.out.println("用户注册成功！！！！");
                    break;
                }
            }
        }
    }

    public static void saveUser(Map<String, String> userMaps,String username,String password){
        try {
            ArrayList<String> userList = new ArrayList<>();
            ArrayList<String> passwordList = new ArrayList<>();
            Set<Map.Entry<String, String>> entries = userMaps.entrySet();
            for (Map.Entry<String, String> entry : entries) {
                userList.add(entry.getKey());
                passwordList.add(entry.getValue());
            }

            StringBuilder userStringBuilder = new StringBuilder("usernames=");
            StringBuilder passwordStringBuilder = new StringBuilder("passwords=");
            for (String n : userList) {
                userStringBuilder.append("|").append(n);
            }
            userStringBuilder.append("|").append(username);

            for (String n : passwordList) {
                passwordStringBuilder.append("|").append(n);
            }
            passwordStringBuilder.append("|").append(password);

            BufferedWriter bw = new BufferedWriter(new FileWriter("java/src/com/shujia/day17/users.properties"));

            bw.write(userStringBuilder.toString());
            bw.newLine();
            bw.write(passwordStringBuilder.toString());
            bw.flush();

            bw.close();

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void login(){
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>登录操作进行中");
        Scanner sc = new Scanner(System.in);
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("######## 欢迎登录李刚博彩城 ^_< ########");
        System.out.print("请输入您的用户名: ");
        String name = sc.nextLine();
        //TODO: 这里需要读取配置文件获取用户和密码
        Map<String, String> userMaps = getUserMaps();
        String pwd = userMaps.get(name);
        System.out.print("请输入您的密码: ");
        String password = sc.nextLine();

        if(pwd==null){
            System.out.println("该用户不存在, 请先注册！");
        }else if(pwd.equals(password)){
            System.out.println("登录成功！美女荷官，在线发牌....");
        }else {
            System.out.println("您输入的密码错误！！！已退出...");
        }
    }

    public static Map<String,String> getUserMaps(){
        Properties prop = null;
        HashMap<String, String> map = new HashMap<>();
        try {
            //创建Properties对象
            prop = new Properties();
            prop.load(new BufferedReader(new FileReader("java/src/com/shujia/day17/users.properties")));
            String usernames = prop.getProperty("usernames");
            String passwords = prop.getProperty("passwords");

            if("".equals(usernames)){
                return map;
            }else {
                String[] usernameArray = usernames.split("\\|");
                String[] passwordArray = passwords.split("\\|");
                for(int i=0;i<usernameArray.length;i++){
                    String name = usernameArray[i];
                    String pwd = passwordArray[i];
                    map.put(name,pwd);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        map.remove("");

        return map;
    }


    public static void main(String[] args) {
        init();
    }
}
